{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# filter函数\n",
    "- 过滤函数：对一组数据进行过滤，符合条件的数据会生成一个新的列表并返回\n",
    "- 跟map比较：\n",
    "    - 相同：都对列表的每一个元素逐一操作\n",
    "    - 不同：\n",
    "        - map会生成一个跟原来队列相对应的队列\n",
    "        - filter不一定，只有符合条件的才会进入新的数据合集\n",
    "- filter函数写法：\n",
    "    - 利用给定函数进行判断\n",
    "    - 返回值一定是一个布尔值\n",
    "    - 调用格式：filter(f, data) f是过滤函数，data是数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "4\n",
      "6\n",
      "6\n",
      "2\n",
      "34\n",
      "444\n"
     ]
    }
   ],
   "source": [
    "def isEven(a):\n",
    "    return a % 2 == 0\n",
    "\n",
    "l = [1,2,3,4,5,6,7,6,5,2,34,43,665,4353,433,444]\n",
    "\n",
    "rst = filter(isEven, l)\n",
    "for i in rst:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 高阶函数-排序\n",
    "- 把一个序列按照一个给定算法进行排序\n",
    "- key：在排序前对每一个元素进行key函数运算，可以理解成按key函数定义的逻辑进行排序\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5343, 2121, 322, 233, 232, 45, 7, 6, 5, 5, 2]\n"
     ]
    }
   ],
   "source": [
    "# 排序按列\n",
    "\n",
    "a = [2121,233,232,5343,2,322,5,6,7,5,45]\n",
    "al = sorted(a, reverse = True)\n",
    "print(al)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 返回函数\n",
    "- 函数可以返回具体的值\n",
    "- 也可以返回一个函数作为结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "in myF\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "def myF(a):\n",
    "    print('in myF')\n",
    "    return None\n",
    "\n",
    "a = myF(8)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'function'>\n",
      "<function myF2.<locals>.myF3 at 0x1032fa7b8>\n",
      "in myF3\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 函数作为返回值返回，被返回的函数在函数体内定义\n",
    "def myF2():\n",
    "    \n",
    "    def myF3():\n",
    "        print('in myF3')\n",
    "        return 3\n",
    "    return myF3\n",
    "\n",
    "f3 = myF2()\n",
    "print(type(f3))\n",
    "print(f3)\n",
    "f3()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 闭包\n",
    "- 当一个函数在内部定义函数，并且内部函数用了外部的函数参数或者局部变量，  当内部函数被当作返回值时，相关参数和变量保存在返回的函数中，这种结果叫闭包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n",
      "hello world\n"
     ]
    }
   ],
   "source": [
    "def hello():\n",
    "    print('hello world')\n",
    "    \n",
    "hello()\n",
    "\n",
    "f = hello\n",
    "f()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 新的需求\n",
    "# 对hello功能进行拓展，每次打印hello之前显示系统时间\n",
    "# 要实现这个功能但是不能改变代码\n",
    "# --》使用装饰器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 装饰器\n",
    "- 在不改动函数代码的基础上无线拓展函数的功能的一种机制，本质上讲，装饰器是一个返回函数的高阶函数\n",
    "- 装饰器的使用：使用@语法，即在每次要拓展的函数定义前使用@+函数名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "# 高阶函数，把函数当参数传入\n",
    "def printTime(f):\n",
    "    def wrapper(*args, **kwargs):\n",
    "        print('time', time.ctime())\n",
    "        return f(*args, **kwargs)\n",
    "    return wrapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time Mon Oct 15 20:49:44 2018\n",
      "hello world\n"
     ]
    }
   ],
   "source": [
    "@printTime\n",
    "def hello():\n",
    "    print('hello world')\n",
    "    \n",
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
